package  com.net
{

	import flash.utils.ByteArray;

	import com.net.interfaces.IData;
	import com.net.interfaces.IDestroy;

	public class DataBase implements IData, IDestroy
	{
		protected var data : ByteArray;

		public function DataBase()
		{
		}

		public function serialize() : ByteArray
		{
			return null;
		}

		public function deSerialize(data : ByteArray) : void
		{
			this.data = data;
		}

		public function getCmd() : int
		{
			return 0;
		}

		/**
		 *
		 * @param value
		 * @param byte
		 * @param type  1int，2byte,3short,4string,5obj
		 *
		 */
		protected function writeArray(value : Array, byte : ByteArray, type : int) : void
		{
			if (value == null)
			{
				byte.writeShort(0);
				return;
			}
			var len : int = value.length;
			var i : int = 0;
			byte.writeShort(len);

			switch (type)
			{
				case 1:
					for (i = 0; i < len; i++)
					{
						byte.writeInt(value[i]);
					}
					break;
				case 2:
					for (i = 0; i < len; i++)
					{
						byte.writeByte(value[i]);
					}
					break;
				case 3:
					for (i = 0; i < len; i++)
					{
						byte.writeShort(value[i]);
					}
					break;
				case 4:
					for (i = 0; i < len; i++)
					{
						byte.writeUTF(value[i]);
					}
					break;
				case 5:
					for (i = 0; i < len; i++)
					{
						byte.writeBytes(value[i].serialize());
					}
					break;
			}
		}


		protected function readArrayInt() : Array
		{
			var tmpArr : Array = [];
			var len : int = data.readShort();

			for (var i : int = 0; i < len; i++)
			{
				tmpArr.push(data.readInt());
			}
			return tmpArr;
		}

		protected function readArrayString() : Array
		{
			var tmpArr : Array = [];
			var len : int = data.readShort();

			for (var i : int = 0; i < len; i++)
			{
				tmpArr.push(data.readUTF());
			}
			return tmpArr;
		}


		protected function readObjectArray(type : Class) : Array
		{
			var tmpArr : Array = [];
			var len : int = data.readShort();

			for (var i : int = 0; i < len; i++)
			{
				var obj : DataBase = readObject(type);

				if (obj)
				{
					tmpArr.push(obj);
				}
			}
			return tmpArr;
		}

		protected function readByteArray() : Array
		{
			var tmpArr : Array = [];
			var len : int = data.readShort();

			for (var i : int = 0; i < len; i++)
			{
				tmpArr.push(data.readUnsignedByte());
			}
			return tmpArr;
		}

		protected function readShortArray() : Array
		{
			var tmpArr : Array = [];
			var len : int = data.readShort();

			for (var i : int = 0; i < len; i++)
			{
				tmpArr.push(data.readUnsignedShort());
			}
			return tmpArr;
		}

		protected function readObject(type : Class) : DataBase
		{
			var obj : DataBase = new type() as DataBase;

			if (obj == null)
			{
				return null;
			}
			obj.deSerialize(data);
			return obj;
		}

		public function get isDestroy() : Boolean
		{
			return false;
		}

		public function destroy() : void
		{
			data = null;
		}

	}
}